package xyz.xtgl.config.helper;

import common.core.util.CommonUtil;
import common.core.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liubo
 * @created 2020-02-03 11:24
 */
@Slf4j
public class MyAuthenctiationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        log.error("登录失败",exception);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(CommonUtil.convertObjectToJson(Result.fail(exception.getMessage())));
    }

}